import Layout from '@/layout/index'
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        // path: 'redirect',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/Login'),
    name: 'login',
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    hidden: true,
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        meta: {
          title: '系统首页', // 标题
          affix: true, // 是否固定(在tabs中是否可以关闭)
          noCache: true // 如果设置为true，则不会被 <keep-alive> 缓存(默认 true)
        },
        component: () => import('@/views/dashboard/index')
      }
    ]
  },
  // 此写法解决动态路由页面刷新的 warning 警告
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/404'),
    hidden: true
  }
]

const router = createRouter({
  history: import.meta.env.MODE === 'development' ? createWebHashHistory() : createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes
})

export function resetRouter() {
  const newRouter = createRouter({
    history: import.meta.env.MODE === 'development' ? createWebHashHistory() : createWebHistory(import.meta.env.BASE_URL),
    routes: constantRoutes
  })
  router.matcher = newRouter.matcher
}

// 全局注册 router
export function setupRouter(app) {
  app.use(router)
}

export default router
